package com.android.sq.zouqi.relationship;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.util.Log;

import com.android.sq.zouqi.Constants;
import com.android.sq.zouqi.util.ServerData;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class pCtrlRelation {
	private final static String TAG = "pCtrlRelation";
	private Context context;
	private Handler handler;
	private SharedPreferences share;
	private ServerData server_data = null;
	private final String url_send_relation_request = "/zouqi/RelationManage/pSendRelationRequest.php";
	private final String url_alert_relation = "/zouqi/RelationManage/pAlertRelation.php";
	private final String url_send_relation_requests = "/zouqi/RelationManage/pSendRelationRequests.php";
	private final String url_get_relation_list = "/zouqi/RelationManage/pGetRelationList.php";
	private String error_msg = "";
	
	public pCtrlRelation(Context context, Handler handler) {
		this.context = context;
		this.handler = handler;
		share = context.getSharedPreferences(Constants.SP_USER_INFO, Context.MODE_PRIVATE);
	}
	
	/*
	 *  获得本机通讯录数据 - pCC03F01
	 *  返回一个list以供填充数据用。
	 *  模拟器无法模拟，暂时搁置
	 */
	//public List<通讯录数据> pGetLocalContacts() {}
	
	/*
	 * 发出好友邀请 - pCC03F02
	 * 在数据库中写入一条好友关系记录，如果写入成功则返回该条记录的系统id，否则返回null值。
	 */
	public int pSendRelationRequest(int _id, int friend_id) {
		int return_id = -1;
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair(Constants.PARAM_USER_ID, "" + _id));
		params.add(new BasicNameValuePair(Constants.PARAM_FRIEND_ID, "" + friend_id));
		
		server_data = new ServerData(Constants.SERVER_URL + url_send_relation_request, params);
		String result_str = server_data.getResultString();
		return_id = Integer.parseInt(result_str);
		
		return return_id;
	}
	
	/*
	 * 确认好友邀请 - pCC03F03
	 * 将数据库相应的记录中关系状态的值改为pTableRelation.RELATION_CONFIRMED(已确认)，
	 * 修改成功则返回该条记录的系统id，否则返回null值。
	 * 返回-1 说明数据库操作失败
	 * 返回0说明记录已经是确认状态，未更新
	 * 返回n说明更新成功
	 * 参数修改说明：因为要修改两条记录的值，所以参数有三个
	 * relation_id = 被邀请的系统id
	 * user_id = 用户id
	 * friend_id = 邀请人的id
	 * 					int id->pTableRelation relation
	 * ######在服务端，UPDATE pTableRelation SET pRelationState = 0 WHERE _id = relation_id
	 * UPDATE pTableRelation SET pRelationState = 0 WHERE pUserId = friend_id and pFriendId = user_id;
	 */
	public int pConfirmRelation(pTableRelation relation) {
		int return_id = -1;
		int relation_id = relation.get_id();
		int user_id = relation.getpUserId();
		int friend_id = relation.getpFriendId();
		//Log.e("pScrMain", "处理的relation：" + relation_id + " uid-" + user_id + " fid-" + friend_id);
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair(Constants.PARAM_RELATION_ID, "" + relation_id));
		params.add(new BasicNameValuePair(Constants.PARAM_FRIEND_ID, "" + friend_id));
		params.add(new BasicNameValuePair(Constants.PARAM_USER_ID, "" + user_id));
		server_data = new ServerData(Constants.SERVER_URL + url_alert_relation, params);
		String result_str = server_data.getResultString();
		return_id = Integer.parseInt(result_str);
		return return_id;
	}
	
	/*
	 * 发出多条好友邀请 - pCC03F04
	 * 循环调用pCC03F02进行新好友关系记录的写入。
	 * 返回值用int代替void，便于测试
	 */
	public int pSendRelationRequests(int user_id, int[] friends_id) {
		int return_id = -1;
		int size = friends_id.length;
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair(Constants.PARAM_USER_ID, "" + user_id));
		params.add(new BasicNameValuePair(Constants.PARAM_RELATION_REQUESTS_COUNT, "" + size));
		for (int i = 0; i < size; i++) {
			params.add(new BasicNameValuePair(Constants.PARAM_FRIEND_ID+i, "" + friends_id[i]));
		}
		server_data = new ServerData(Constants.SERVER_URL + url_send_relation_requests, params);
		String result_str = server_data.getResultString();
		return_id = Integer.parseInt(result_str);
		return return_id;
	}
	
	/*
	 * 获得好友关系列表数据 - pCC03F05
	 * 关系状态参数使用pTableRelation.RELATION_UNCONFIRM或pTableRelation.RELATION_CONFIRMED
	 * 分别取得指定用户id未确认邀请的好友列表或已确认邀请的好友列表数据，
	 * 返回一个好友关系类的数组。
	 */
	public LinkedList<pTableRelation> pGetRelationList(int user_id, int relation_state) {
		Log.e("pCtrlRelation", "user_id = " + user_id);
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair(Constants.PARAM_USER_ID, "" + user_id));
		params.add(new BasicNameValuePair(Constants.PARAM_RELATION_STATE, "" + relation_state));
		server_data = null;
		server_data = new ServerData(Constants.SERVER_URL + url_get_relation_list, params);
		String json_data = server_data.getResultJsonString();
		//Log.e(TAG, "JSON_DATA = " + json_data);
		
		return getRelationList(json_data);
		/*int size = relations_list.size();
		pTableRelation relations[] = new pTableRelation[size];
		int i = 0;
		for (Iterator<pTableRelation> it = relations_list.iterator(); it.hasNext();) {
			pTableRelation relation = it.next();
			relations[i++] = relation;
		}
		return relations;*/
	}
	
	public LinkedList<pTableRelation> getRelationList(String json_str) {
		Type listType = new TypeToken<LinkedList<pTableRelation>>() {}.getType();
		Gson gson = new Gson();
		Log.e("BA", "json_str = " + json_str);
		LinkedList<pTableRelation> relations_list = gson.fromJson(json_str, listType);
		Log.e(TAG, "好友数有" + relations_list.size());
		return relations_list;
	}
}
